package cn.wolfcode.wechat.controller;


import cn.wolfcode.wechat.domain.Account;
import cn.wolfcode.wechat.domain.Client;
import cn.wolfcode.wechat.domain.Recharge;
import cn.wolfcode.wechat.query.RechargeQueryObject;
import cn.wolfcode.wechat.service.IClientService;
import cn.wolfcode.wechat.service.IRechargeService;
import cn.wolfcode.wechat.util.AccessTokenUtil;
import cn.wolfcode.wechat.util.HttpUtil;
import cn.wolfcode.wechat.util.JSONResult;
import cn.wolfcode.wechat.util.UrlUtil;
import cn.wolfcode.wechat.vo.TemplateValueVo;
import cn.wolfcode.wechat.vo.TemplateVo;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class RechargeController {
    @Value("${wechat.appId}")
    private String appid;
    @Value("${wechat.appSecret}")
    private String appsecret;

    private String openId;
    private Long cilentId;

    @Autowired
    private IRechargeService rechargeService;
    @Autowired
    private IClientService clientService;


    @RequestMapping("/recharge")
    public String recharge(){
        return "recharge/recharge";
    }

    @RequestMapping("/rechargeSave")
    @ResponseBody
    public JSONResult rechargeSave(Recharge recharge){
        JSONResult result = null;
        try {
            openId = WechatControllor.OPENID;
            rechargeService.rechargeSave(recharge, openId);
            result = new JSONResult();
        }catch (Exception e){
            e.printStackTrace();
            result = new JSONResult(false, e.getMessage() );
        }
        return result;
    }

    /**
     * 后台审核页面
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/rechargePage")
    public String rechargePage(@ModelAttribute("qo")RechargeQueryObject qo, Model model){
        PageInfo pageResult = rechargeService.queryPage(qo);
        model.addAttribute("pageResult", pageResult);
        return "recharge/rechargelist";
    }

    /**
     * 个人账户页面
     * @param model
     * @return
     */
    @RequestMapping("/accountPage")
    public String accountPage(Model model){
        openId = WechatControllor.OPENID;
        Client client = rechargeService.getClientByOpenId(openId);
        Account account = rechargeService.getAccountByClientId(client.getId());
        model.addAttribute("client", client);
        model.addAttribute("account", account);
        return "recharge/account";
    }

    @RequestMapping("/rechargeOffline_audit")
    @ResponseBody
    public JSONResult rechargeOffline_audit(Long id, int state, String remark){
        JSONResult result = null;
        try {
            rechargeService.audit(id, state, remark);
            result = new JSONResult();
        }catch (Exception e){
            e.printStackTrace();
            result = new JSONResult(false, e.getMessage() );
        }
        return result;
    }

    /**
     * 审核后发送的模板
     * @param id
     * @return
     */
    @RequestMapping("/rechargeTemplate")
    @ResponseBody
    public TemplateVo rechargeTemplate(Long id){
        Recharge recharge = rechargeService.get(id);
        Client client = clientService.get(recharge.getClient().getId());
        TemplateVo templateVo = new TemplateVo();
        templateVo.setTemplate_id("NPqSW2BqCyCqfpLhlwO3H8V8qOSoQ7-hyBZTn0S4RmA");
        templateVo.setTouser(client.getOpenid());
        templateVo.setUrl("");

        Map<String,TemplateValueVo> data = new HashMap<>();
        data.put("nickName",new TemplateValueVo(recharge.getClient().getNickname(),"#173177"));
        data.put("tradeCode",new TemplateValueVo(recharge.getTradeCode(),"#173177"));
        data.put("amount",new TemplateValueVo(recharge.getAmount().toString(),"#173177"));
        data.put("auditTime",new TemplateValueVo( recharge.getAuditTime().toLocaleString(),"#173177"));
        data.put("note",new TemplateValueVo(recharge.getNote(),"#173177"));
        templateVo.setData(data);
        //获取access_token
        String access_token = AccessTokenUtil.getAccessTokenVo();
        /*String accesstUrl = UrlUtil.ACCESS_TOKEN_URL+"?grant_type=client_credential&appid="+appid+"&secret="+appsecret;
        String accessToken = HttpUtil.get(accesstUrl);
        AccessVo accessVo = JSON.parseObject(accessToken, AccessVo.class);
        String access_token = accessVo.getAccess_token();
        //String accesstoken = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;*/

        String templateUrl = UrlUtil.TEMPLATE_SEND_URL + "?access_token="+access_token;
        String ret = HttpUtil.post(templateUrl,JSON.toJSONString(templateVo));
        System.out.println(ret);
        return templateVo;
    }

}
